package com.lottery.im.module.result.ui.statistics;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import butterknife.BindView;
import com.lottery.im.R;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.Constants;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.result.adapter.result.statistics.Double115Adapter;
import com.lottery.im.module.result.adapter.result.statistics.DoubleLotteryAdapter;
import com.lottery.im.module.result.adapter.result.statistics.DoublePkAdapter;
import com.lottery.im.module.result.presenter.DoubleStatisticsPresenter;
import com.lottery.im.module.result.view.DoubleStatisticsView;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.util.rxbus.Subscribe;
import java.util.ArrayList;
import java.util.List;

/**
 * 双面统计
 */
public class DoubleStatisticsFragment extends BaseSwipeFragment<DoubleStatisticsPresenter>
    implements DoubleStatisticsView {
  private final static String GAME_TYPE_KEY = "GAME_TYPE_KEY";

  public static DoubleStatisticsFragment create(String gameType) {
    DoubleStatisticsFragment fragment = new DoubleStatisticsFragment();
    Bundle b = new Bundle();
    b.putString(GAME_TYPE_KEY, gameType);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.double_recycler) RecyclerView mRecyclerView;
  private List<String> mDoubleList = new ArrayList<>();
  private DoublePkAdapter mPkAdapter;
  private Double115Adapter m115Adapter;
  private DoubleLotteryAdapter mAdapter;
  private String mGameType;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_statistics_double;
  }

  @Override protected void initPresenter() {
    presenter = new DoubleStatisticsPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    if (getArguments() == null) {
      popBackStack();
      return;
    } else {
      RxBus.get().register(this);
      mGameType = getArguments().getString(GAME_TYPE_KEY);
      mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
      if (Constants.pkArr.contains(mGameType)) {
        mPkAdapter = new DoublePkAdapter(mDoubleList);
        mRecyclerView.setAdapter(mPkAdapter);
      } else if (Constants.sd115Arr.contains(mGameType)) {
        m115Adapter = new Double115Adapter(mDoubleList);
        mRecyclerView.setAdapter(m115Adapter);
      } else {
        mAdapter = new DoubleLotteryAdapter(mDoubleList);
        mRecyclerView.setAdapter(mAdapter);
      }
      presenter.queryDXDSLost(mGameType, "1");
    }
  }

  @Override public void queryDxDsSuccess(List<String> list) {
    mDoubleList.clear();
    if (Constants.pkArr.contains(mGameType)) {
      for (int i = 3; i < list.size(); i++) {
        mDoubleList.add(list.get(i));
      }
      if (null != mPkAdapter)
      mPkAdapter.notifyDataSetChanged();
    } else {
      for (int i = 2; i < list.size(); i++) {
        mDoubleList.add(list.get(i));
      }
      if (null != m115Adapter)
        m115Adapter.notifyDataSetChanged();
      if (null != mAdapter)
        mAdapter.notifyDataSetChanged();
    }
  }

  /*
   * 刷新开奖结果结果
   * */
  @Subscribe(code = RxBusCode.REFRESH_CODE)
  public void refresh() {
    if (null == mActivity || mActivity.isFinishing()) {
      return;
    }
    presenter.queryDXDSLost(mGameType, "1");
  }

  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }
}
